import 'package:boss/ui/page/ModulePage/module_page.dart';
import 'package:boss/ui/page/analysis/analysis_page.dart';
import 'package:boss/ui/page/index/model/bottom_navigation.dart';
import 'package:boss/ui/page/index/widget/custom_bottom_navigation_bar.dart';
import 'package:boss/ui/page/personalSettings/personal_setting_page.dart';
import 'package:boss/ui/page/reportForm/report_page.dart';
import 'package:boss/ui/widget/quit_app.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';
import 'package:router/annotation.dart';

@RouterConfig(IndexPage.pageName)
class IndexPage extends StatefulWidget {
  static const pageName = '/indexPage';

  @override
  _IndexPageState createState() => _IndexPageState('主页面');
}

class _IndexPageState extends BaseState<IndexPage> {
  int _currentIndex = 0;
  List<BottomNavigation> _bottomNavigationBarItems = [
    BottomNavigation(
      title: '首页',
      icon: 'assets/image/bottom_navigation_home_icon.png',
      selectedIcon: 'assets/image/bottom_navigation_home_on_icon.png',
    ),
    BottomNavigation(
      title: '模块',
      icon: 'assets/image/bottom_navigation_module_icon.png',
      selectedIcon: 'assets/image/bottom_navigation_module_on_icon.png',
    ),
    BottomNavigation(
      title: '报表',
      icon: 'assets/image/bottom_navigation_account_icon.png',
      selectedIcon: 'assets/image/bottom_navigation_account_on_icon.png',
    ),
    BottomNavigation(
      title: '我的',
      icon: 'assets/image/bottom_navigation_personal_setting_icon.png',
      selectedIcon: 'assets/image/bottom_navigation_personal_setting_on_icon.png',
    ),
  ];

  GlobalKey<PersonalSettingPageState> _personalSettingPageKey;

  List<Widget> _contentPages;

  _IndexPageState(String pageName) : super(pageName);

  @override
  void initState() {
    super.initState();

    _personalSettingPageKey = GlobalKey<PersonalSettingPageState>();

    _contentPages = [
      AnalysisPage(),
      ModulePage(),
      ReportPage(),
      PersonalSettingPage(
        key: _personalSettingPageKey,
      ),
    ];
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil(width: 750, height: 1334).init(context);

    return QuitApp(
      child: Scaffold(
        body: IndexedStack(
          index: _currentIndex,
          children: _contentPages,
        ),
        bottomNavigationBar: CustomBottomNavigationBar(
          items: _bottomNavigationBarItems,
          selectedIndex: _currentIndex,
          onSelectChanged: _changeIndexHandle,
        ),
      ),
    );
  }

  _changeIndexHandle(int index) {
    setState(() {
      _currentIndex = index;

      if (index == 1) {
      } else if (index == 2) {
      } else if (index == 3) {
        _personalSettingPageKey.currentState.initData();
      }
    });
  }
}
